/*
 * 路由文件
 */
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var sql = require('./mysql.js');

//链接数据库
var connection = mysql.createConnection({
	host:'localhost',
	user:'root',
	password:'',
	database:'guomei'
});
//建立连接
connection.connect();
//返回信息
var msg = {"err": 0, "msg":'失败'};
var success = {"err": 1, "msg":'成功'};

//获取数据
router.get('/select',function(req,res,next){	
	var data = req.query; //前台数据
	console.log(data);
	connection.query( data.condition,[data.phone, data.psw],function(err,result,failed){
		//解决跨域问题
		res.setHeader("Access-Control-Allow-Origin",'*');
		res.setHeader("Access-Control-Allow-Methods",'get,post,put,options');		
		if(!err){ //成功			
			console.log(success);
			result.msg=success;
			res.send(result);
		}else{ //失败			
			console.log(err);
			result.msg=err;
			res.send(msg);
		}
	})
});

//添加数据
router.get('/add',function(req,res,next){			
	var data = req.query; //前台数据
	console.log(data);
	connection.query( data.condition,[data.phone,data.psw],function(err,result,failed){
		//解决跨域问题
		res.setHeader("Access-Control-Allow-Origin",'*');
		res.setHeader("Access-Control-Allow-Methods",'get,post,put,options');
		if(!err){ //成功	
			result.msg = success;
			console.log(success);
			res.send(result);
		}else{//失败	
			result.msg = err;
			console.log(err);
			res.send(msg);
		}
	})
});

//更新数据
router.get('/update',function(req,res,next){	
	var data = req.query; //前台数据
	console.log(data);
	connection.query( data.condition,[data.psw,data.phone],function(err,result,failed){
		//解决跨域问题
		res.setHeader("Access-Control-Allow-Origin",'*');
		res.setHeader("Access-Control-Allow-Methods",'get,post,put,options');
		if(!err){ //成功			
			console.log(success);
			result.msg = success;
			res.send(result);
		}else{//失败			
			console.log(err);
			result.msg = err;
			res.send(msg);
		}
	})
});

//删除数据
router.get('/remove',function(req,res,next){	
	var data = req.query; //前台数据	
	console.log(data);
	connection.query( data.condition,data.id,function(err,result,failed){
		//解决跨域问题
		res.setHeader("Access-Control-Allow-Origin",'*');
		res.setHeader("Access-Control-Allow-Methods",'get,post,put,options');
		if(!err){ //成功			
			console.log(success);
			result.msg = success;
			res.send(result);
		}else{//失败			
			console.log(err);
			result.msg = err;
			res.send(msg);
		}
	})
});
//查询
router.get('/chaxun',function(req,res,next){	
	var data = req.query; //前台数据
	console.log(data);
	connection.query( data.condition,[data.phone],function(err,result,failed){
		//解决跨域问题
		res.setHeader("Access-Control-Allow-Origin",'*');
		res.setHeader("Access-Control-Allow-Methods",'get,post,put,options');		
		if(!err){ //成功			
			console.log(success);
			result.msg=success;
			res.send(result);
		}else{ //失败			
			console.log(err);
			result.msg=err;
			res.send(msg);
		}
	})
});

router.get('/shop',function(req,res,next){			
	var data = req.query; //前台数据
	console.log(data);
	
	connection.query( data.condition,[data.phone,data.src,data.goods,data.type,data.num,data.place,data.price,data.muchMoney],function(err,result,failed){
		//解决跨域问题
		res.setHeader("Access-Control-Allow-Origin",'*');
		res.setHeader("Access-Control-Allow-Methods",'get,post,put,options');
		if(!err){ //成功	
			result.msg = success;
			console.log(success);
			res.send(result);
		}else{//失败	
			result.msg = err;
			console.log(err);
			res.send(msg);
		}
	})
});




module.exports = router; 

 